<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>libdill</title>
  <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>

<h1>libdill: Structured Concurrency for C</h1>

<ul id='toplist'>
<li><a href="index.html">Home</a></li>
<li><a href="download.html">Download</a></li>
<li><a href="documentation.html">Documentation</a></li>
<li><a href="tutorial.html">Tutorials</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
<h1 id="name">NAME</h1>
<p>tcp_connect - creates a connection to remote TCP endpoint</p>
<h1 id="synopsis">SYNOPSIS</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="pp">#include </span><span class="im">&lt;libdill.h&gt;</span>

<span class="dt">int</span> tcp_connect(
    <span class="dt">const</span> <span class="kw">struct</span> ipaddr* addr,
    <span class="dt">int64_t</span> deadline);</code></pre></div>
<h1 id="description">DESCRIPTION</h1>
<p>TCP protocol is a reliable bytestream protocol for transporting data over network. It is defined in RFC 793.</p>
<p>This function creates a connection to a remote TCP endpoint.</p>
<p><strong>addr</strong>: IP address to connect to.</p>
<p><strong>deadline</strong>: A point in time when the operation should time out, in milliseconds. Use the <strong>now</strong> function to get your current point in time. 0 means immediate timeout, i.e., perform the operation if possible or return without blocking if not. -1 means no deadline, i.e., the call will block forever if the operation cannot be performed.</p>
<p>The socket can be cleanly shut down using <strong>tcp_close</strong> function.</p>
<p>This function is not available if libdill is compiled with <strong>--disable-sockets</strong> option.</p>
<h1 id="return-value">RETURN VALUE</h1>
<p>In case of success the function returns newly created socket handle. In case of error it returns -1 and sets <strong>errno</strong> to one of the values below.</p>
<h1 id="errors">ERRORS</h1>
<ul>
<li><strong>ECANCELED</strong>: Current coroutine was canceled.</li>
<li><strong>ECONNREFUSED</strong>: The target address was not listening for connections or refused the connection request.</li>
<li><strong>ECONNRESET</strong>: Remote host reset the connection request.</li>
<li><strong>EHOSTUNREACH</strong>: The destination host cannot be reached.</li>
<li><strong>EINVAL</strong>: Invalid argument.</li>
<li><strong>EMFILE</strong>: The maximum number of file descriptors in the process are already open.</li>
<li><strong>ENETDOWN</strong>: The local network interface used to reach the destination is down.</li>
<li><strong>ENETUNREACH</strong>: No route to the network is present.</li>
<li><strong>ENFILE</strong>: The maximum number of file descriptors in the system are already open.</li>
<li><strong>ENOMEM</strong>: Not enough memory.</li>
<li><strong>ETIMEDOUT</strong>: Deadline was reached.</li>
</ul>
<h1 id="example">EXAMPLE</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="kw">struct</span> ipaddr addr;
ipaddr_remote(&amp;addr, <span class="st">&quot;www.example.org&quot;</span>, <span class="dv">80</span>, <span class="dv">0</span>, <span class="dv">-1</span>);
<span class="dt">int</span> s = tcp_connect(&amp;addr, <span class="dv">-1</span>);
bsend(s, <span class="st">&quot;ABC&quot;</span>, <span class="dv">3</span>, <span class="dv">-1</span>);
<span class="dt">char</span> buf[<span class="dv">3</span>];
brecv(s, buf, <span class="kw">sizeof</span>(buf), <span class="dv">-1</span>);
tcp_close(s);</code></pre></div>
<h1 id="see-also">SEE ALSO</h1>
<p><strong>brecv</strong>(3) <strong>brecvl</strong>(3) <strong>bsend</strong>(3) <strong>bsendl</strong>(3) <strong>hclose</strong>(3) <strong>now</strong>(3) <strong>tcp_accept</strong>(3) <strong>tcp_accept_mem</strong>(3) <strong>tcp_close</strong>(3) <strong>tcp_connect_mem</strong>(3) <strong>tcp_done</strong>(3) <strong>tcp_fromfd</strong>(3) <strong>tcp_fromfd_mem</strong>(3) <strong>tcp_listen</strong>(3) <strong>tcp_listen_mem</strong>(3) <strong>tcp_listener_fromfd</strong>(3) <strong>tcp_listener_fromfd_mem</strong>(3)</p>
</body>
